/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-25 10:24:20
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-02-02 15:52:55
 */

import baozhaEle from "../eles/baozhaEle";
import BaseEle, { campType, soliderType } from "../eles/baseEle";
import bulletEle from "../eles/bulletEle";
import circleEle from "../eles/circleEle";
import descComp from "../views/descCom";
import dieEle from "../eles/dieEle";
import gameCtr, { modelType } from "../ctrs/gameCtr";
import lineCom from "../eles/lineCom";
import mouseCom from "../views/mouseCom";
import { SoundResManager } from "../ctrs/soundCtr";
import triangleCom from "../eles/triangleCom";
import arrowEle from "../eles/arrowEle";
import fireBallEle from "../eles/fireBallEle";
import squareCom from "../eles/squareCom";
import frozenEle from "../eles/frozenEle";
import recoverEle from "../eles/recoverEle";
import BaseGameScene from "../libs/core/BaseGameScene";
import { SceneManager } from "../libs/manager/SceneManager";
import barrackEntity from "../ctrs/barracks/barrackEntity";
import barrackCtr from "../ctrs/barracks/barrackCtr";
import snowEle from "../eles/snowEle";
import treasureCtr from "../ctrs/treasure/treasureCtr";
import exchangeTp from "../ctrs/treasure/exchangeTp";
import netCtr from "../ctrs/netCtr";
import treasureTp from "../ctrs/treasure/treasureTp";
import dcEle from "../eles/dcEle";
import helpCtr from "../ctrs/helps/helpCtr";
import bagCtr from "../ctrs/bags/bagCtr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class mainScene extends BaseGameScene {

    @property({ type: cc.Node, tooltip: "鼠标节点" })
    mouseNode: cc.Node = null;

    @property({ type: cc.Animation, tooltip: "鼠标动画" })
    mouseAni: cc.Animation = null;

    @property({ type: cc.Node, tooltip: "元素节点" })
    eleNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: "资源节点" })
    resNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: "buff节点" })
    effectNode: cc.Node = null;

    @property({ type: cc.Prefab, tooltip: "miner预制体" })
    minerPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "spearman预制体" })
    spearmanPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "guard预制体" })
    guardPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "shield预制体" })
    shieldPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "castle预制体" })
    castlePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "militia预制体" })
    militiaPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "bowman预制体" })
    bowmanPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "swordsman预制体" })
    swordsmanPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "cavalry预制体" })
    cavalryPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "trapped预制体" })
    trappedPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "dogs预制体" })
    dogsPre: cc.Prefab = null;


    @property({ type: cc.Prefab, tooltip: "master预制体" })
    masterPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "fighter预制体" })
    fighterPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "warlock预制体" })
    warlockPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "scarecrow预制体" })
    scarecrowPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "jumpFire预制体" })
    jumpFirePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "logistics预制体" })
    logisticsPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "树预制体" })
    treePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "矿预制体" })
    mineralPre: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: "圈的节点" })
    circleNode: cc.Node = null;

    @property({ type: cc.Prefab, tooltip: "圈预制体" })
    circle2Pre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "圈预制体" })
    circlePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "三角预制体" })
    trianglePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "四边预制体" })
    squarePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "圈预制体2" })
    circle3Pre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "线预制体" })
    linePre: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: "兵营列表" })
    barracks: cc.Node = null;

    @property({ type: cc.Node, tooltip: "描述节点" })
    descNode: cc.Node = null;

    @property({ type: cc.Label, tooltip: "金币" })
    goldTxt: cc.Label = null;

    @property({ type: cc.Prefab, tooltip: "怪物1预制体" })
    mon1Pre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "怪物2预制体" })
    mon2Pre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "怪物3预制体" })
    mon3Pre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "怪物4预制体" })
    mon4Pre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "加农炮预制体" })
    cannonPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "子弹预制体" })
    bulletPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "火球预制体" })
    fireBallPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "雪花预制体" })
    snowPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "箭预制体" })
    arrowPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "地雷预制体" })
    shellPre: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: "子弹层" })
    bulletNode: cc.Node = null;

    @property({ type: cc.Animation, tooltip: "震动动画" })
    shakeAni: cc.Animation = null;

    @property({ type: cc.Node, tooltip: "是否结束" })
    overNode: cc.Node = null;

    @property({ type: cc.Label, tooltip: "积分" })
    scoreTxt: cc.Label = null;

    @property({ type: cc.Label, tooltip: "防御时间" })
    fyTimeTxt: cc.Label = null;

    @property({ type: cc.Sprite, tooltip: "奖励icon" })
    icon: cc.Sprite = null;

    @property({ type: cc.Label, tooltip: "奖励描述" })
    rewardTxt: cc.Label = null;

    @property({ type: cc.Button, tooltip: "重新开始按钮" })
    aginBtn: cc.Node = null;

    @property({ type: cc.Prefab, tooltip: "死亡" })
    diePre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "爆炸" })
    baozhaPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "地刺" })
    dcPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "冰冻" })
    frozenPre: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: "回复" })
    recoverPre: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: "血迹层" })
    bloodLayer: cc.Node = null;

    @property({ type: cc.Node, tooltip: "ready" })
    readyNode: cc.Node = null;

    @property({ type: cc.Button, tooltip: "暂停按钮" })
    pauseBtn: cc.Node = null;

    @property({ type: cc.Button, tooltip: "音乐按钮" })
    musicBtn: cc.Node = null;

    @property({ type: cc.Node, tooltip: "火的节点" })
    fireNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: "尾焰" })
    lzNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: "三角圆形" })
    triNode: cc.Node = null;

    @property({ type: cc.Label, tooltip: "游戏时间" })
    gameTimeTxt: cc.Label = null;

    @property({ type: cc.Prefab, tooltip: "地雷预制体" })
    dlPre: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: "道具节点" })
    djNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: "助力节点" })
    helpNode: cc.Node = null;

    public monsterTime: any = false;

    public isPause: boolean = false; //是否暂停

    public timeHandler = null;

    public castleSpr: BaseEle = null; //城堡脚本

    @property(cc.Node)
    bg: cc.Node = null;
    material: cc.Material = null;
    center: number[] = [0.315, 0.9];
    ydTime: any = null;
    typerTimer: any = null;

    @property({ type: cc.Label, tooltip: "打印的文字" })
    dyTxt: cc.Label = null;

    makeLaberTyper(str: string) {
        let charArr = str.split('');
        let charIdx = 0;

        this.typerTimer && clearInterval(this.typerTimer);
        this.typerTimer = setInterval(() => {
            if (charIdx >= charArr.length) {
                this.typerTimer && clearInterval(this.typerTimer);
            } else {
                charIdx += 1;
                this.dyTxt.string = charArr.slice(0, charIdx).join('');
            }
        }, 50);
    }

    onLoad() {
        super.onLoad();
        gameCtr.ins.curScene = this.node;
        gameCtr.ins.resNode = this.resNode;
        gameCtr.ins.effectNode = this.effectNode;
        gameCtr.ins.eleNode = this.eleNode;
        gameCtr.ins.gameModel = modelType.pve;

        this.material = this.bg.getComponent(cc.Sprite).getMaterial(0);
        this.material.setProperty('wh_ratio', this.bg.width / this.bg.height);
        this.material.setProperty('center', this.center);

        if (gameCtr.ins.guideId == 2) {
            this.doGuide();
        } else {
            let material = this.bg.getComponent(cc.Sprite).getMaterial(1);
            this.bg.getComponent(cc.Sprite).setMaterial(0, material);
            this.dyTxt.node.active = false;
        }

        // this.bg.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);

        SoundResManager.getInstance().preload("sound");

        this.node.on("doShakeAni", this.doShakeAni, this);
        this.node.on("gameOver", this.doGameOver, this);
        this.node.on("drawReward", this.drawReward, this);
        this.node.on("die", this.onCreateDie, this);
        this.node.on("fireBullet", this.createBullet, this);
        this.node.on("fireSnow", this.createSnow, this);
        this.node.on("fireBall", this.createBall, this);
        this.node.on("fireArrow", this.createArrow, this);
        this.node.on("baozha", this.onCreateBaozha, this);
        this.node.on("dc", this.onCreateDc, this);
        this.node.on("doShell", this.createShell, this);
        this.node.on("frozen", this.doCreateFrozen, this);
        this.node.on("recover", this.doCreateRecover, this);

        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onMove, this);
        this.node.on(cc.Node.EventType.MOUSE_MOVE, this.onMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onUp, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onUp, this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onUp, this);
        this.node.on("updateGold", this.updateGold, this);
        this.node.on("onHelpClick", this.onHelpClick, this);

        this.node.on("onDjNode", this.onDjNode, this);

        gameCtr.ins.initPool("miner", 10, this.minerPre);
        gameCtr.ins.initPool("spearman", 10, this.spearmanPre);
        gameCtr.ins.initPool("guard", 10, this.guardPre);
        gameCtr.ins.initPool("shield", 10, this.shieldPre);
        gameCtr.ins.initPool("militia", 10, this.militiaPre);
        gameCtr.ins.initPool("bowman", 10, this.bowmanPre);
        gameCtr.ins.initPool("swordsman", 10, this.swordsmanPre);

        gameCtr.ins.initPool("cavalry", 10, this.cavalryPre);
        gameCtr.ins.initPool("trapped", 10, this.trappedPre);
        gameCtr.ins.initPool("dogs", 10, this.dogsPre);
        gameCtr.ins.initPool("master", 10, this.masterPre);
        gameCtr.ins.initPool("fighter", 10, this.fighterPre);
        gameCtr.ins.initPool("warlock", 10, this.warlockPre);
        gameCtr.ins.initPool("scarecrow", 10, this.scarecrowPre);
        gameCtr.ins.initPool("jumpFire", 10, this.jumpFirePre);
        gameCtr.ins.initPool("logistics", 10, this.logisticsPre);

        gameCtr.ins.initPool("mon1", 10, this.mon1Pre);
        gameCtr.ins.initPool("mon2", 10, this.mon2Pre);
        gameCtr.ins.initPool("mon3", 10, this.mon3Pre);
        gameCtr.ins.initPool("mon4", 10, this.mon4Pre);
        gameCtr.ins.initPool("triangle", 5, this.trianglePre);
        gameCtr.ins.initPool("square", 5, this.squarePre);
        gameCtr.ins.initPool("circle", 5, this.circlePre);
        gameCtr.ins.initPool("circle2", 5, this.circle2Pre);
        gameCtr.ins.initPool("circle3", 5, this.circle3Pre);
        gameCtr.ins.initPool("line", 5, this.linePre);
        gameCtr.ins.initPool("tree", 10, this.treePre);
        gameCtr.ins.initPool("mineral", 10, this.mineralPre);
        gameCtr.ins.initPool("die", 20, this.diePre);
        gameCtr.ins.initPool("bullet", 10, this.bulletPre);
        gameCtr.ins.initPool("snow", 10, this.snowPre);
        gameCtr.ins.initPool("cannon", 10, this.cannonPre);
        gameCtr.ins.initPool("baozha", 10, this.baozhaPre);
        gameCtr.ins.initPool("dc", 10, this.dcPre);
        gameCtr.ins.initPool("shell", 10, this.shellPre);
        gameCtr.ins.initPool("dl", 10, this.dlPre);
        gameCtr.ins.initPool("arrow", 10, this.arrowPre);
        gameCtr.ins.initPool("fireBall", 10, this.fireBallPre);
        gameCtr.ins.initPool("frozen", 5, this.frozenPre);
        gameCtr.ins.initPool("recover", 5, this.recoverPre);

        this.updateGold();

        let childs = this.barracks.children;
        for (let i = 0; i < childs.length; i++) {
            if (gameCtr.ins.barracksArr[i]) {
                childs[i].on(cc.Node.EventType.TOUCH_START, this.onBarrckDown, this);

                cc.resources.load(gameCtr.ins.barracksArr[i].iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
                    childs[i].getComponentInChildren(cc.Sprite).spriteFrame = sp;
                })
            } else {
                childs[i].getComponentInChildren(cc.Sprite).spriteFrame = null;
            }
        }

        this.fireNode.on(cc.Node.EventType.TOUCH_START, this.onFire, this);

        this.initEle();

        this.schedule(() => {
            if (gameCtr.ins.isOver) return;
            if (this.isPause) return;
            if (!this.monsterTime) return;
            //自动增加金币的上限+5（由每秒5秒10变为每5秒15
            gameCtr.ins.gold += (10 + bagCtr.ins.goldAdd);
        }, 5)

        this.scheduleOnce(() => {
            this.lzNode.active = false;
        }, 1)

        SceneManager.getInstance().pushLayer("helpLayer");

        let arr = [];

        bagCtr.ins.bagDic.getValueList().forEach(ele => {
            if (ele.time > 0) {
                arr.push(ele);
            }
        })

        bagCtr.ins.bagArr = arr;
    }

    public onDisable(): void {
        super.onDisable();
        clearInterval(this.typerTimer);
        clearInterval(this.ydTime);
        clearTimeout(this.timeHandler);
        this.unscheduleAllCallbacks();
        /**
         * 助力重置
         */
        helpCtr.ins.curHelpEntity = null;

        /**
         * 道具重置
         */
        bagCtr.ins.curEny = null;
        bagCtr.ins.bagArr = [];
        bagCtr.ins.reset();
    }

    doGuide() {
        if (gameCtr.ins.guideId == 2) {
            this.dyTxt.string = "";
            let str = "拖动训练犬\n到矿石区域，放下开始挖矿，训练犬会为你挖金币;\n当将三个矿石挖完，请做好迎接敌人的准备!";
            this.makeLaberTyper(str);
            this.center = [0.315, 0.9];
            this.scheduleOnce(() => {
                this.ydTime = setInterval(() => {
                    if (this.center[0] <= 0.23) {
                        clearInterval(this.ydTime);
                        this.scheduleOnce(() => {
                            this.doGuide();
                        }, 1)
                    }
                    this.center[0] += (-1) / this.bg.width;
                    this.center[1] -= (4) / this.bg.height;
                    this.material.setProperty('center', this.center);
                }, 5);
            }, 1)
        } else if (gameCtr.ins.guideId == 3) {
            this.dyTxt.string = "";
            let str = "拖动枪兵\n到城堡周围，放下开始守卫，枪兵会为你守卫城堡;";
            this.makeLaberTyper(str);
            this.center = [0.39, 0.9];
            this.scheduleOnce(() => {
                this.ydTime = setInterval(() => {
                    if (this.center[0] > 0.5) {
                        clearInterval(this.ydTime);
                        this.scheduleOnce(() => {
                            this.doGuide();
                        }, 1)
                    }
                    this.center[0] += (2) / this.bg.width;
                    this.center[1] -= (4) / this.bg.height;
                    this.material.setProperty('center', this.center);
                }, 5);
            }, 1)
        }
    }

    /**
     * 创建术士
     */
    async createSnow(pos, targetPos, aoeNum, type: soliderType = soliderType.cannon, campType) {
        // console.log(pos, targetPos);
        if (pos && targetPos) {
            let node: cc.Node = gameCtr.ins.poolObj['snow'].get();

            if (!node) {
                node = await cc.instantiate(this.snowPre);
            }

            node.setPosition(pos.x, pos.y);
            // node.angle = Math.random() * 20 - 10;
            let sprn = node.getComponent(snowEle);
            sprn.setTargetPos(pos, targetPos, aoeNum, type, campType);
            this.bulletNode.addChild(node);
        }

    }
    /**
     * 创建冰冻
     */
    doCreateFrozen(pos, campType) {
        let node = gameCtr.ins.poolObj['frozen'].get();
        if (!node) {
            node = cc.instantiate(this.frozenPre);
        }
        let nPos = gameCtr.ins.effectNode.convertToNodeSpaceAR(pos);
        node.setPosition(nPos);
        node.getComponent(frozenEle).setInfo({
            life: 120,
            campType: campType,
        })
        gameCtr.ins.effectNode.addChild(node);
    }

    /**
     * 创建回复
     */
    async doCreateRecover(pos, targetPos, aoeNum, type: soliderType = soliderType.master) {
        if (pos && targetPos) {
            let node: cc.Node = gameCtr.ins.poolObj['recover'].get();

            if (!node) {
                node = await cc.instantiate(this.fireBallPre);
            }

            node.setPosition(pos.x, pos.y);

            // node.angle = Math.random() * 20 - 10;
            let sprn = node.getComponent(recoverEle);
            sprn.setTargetPos(pos, targetPos, aoeNum, type, campType);
            this.bulletNode.addChild(node);
        }
    }

    /**
     * 点击火
     */
    onFire() {
        if (this.isPause) return;
        if (gameCtr.ins.isOver) return;
        if (!this.monsterTime) return;
        if (gameCtr.ins.fireIntel < 900) return;
        this.castleSpr.setFireAni("fire2");
        gameCtr.ins.fireIntel = 0;
        gameCtr.ins.isFire = true;
        // this.mouseNode.getComponent(mouseCom).playAni("fire2");
        this.mouseNode.getComponent(mouseCom).drawSp("ele/lz");
        // console.log("ele/lz")
        this.lzNode.active = true;
        // console.log(this.lzNode.active, "this.lzNode.active")
        clearTimeout(this.timeHandler);
        this.timeHandler = setTimeout(() => {
            gameCtr.ins.isFire = false;
            this.lzNode.active = false;
            this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorIdle");
            this.mouseNode.getComponent(mouseCom).playAni("mouseIdle");
        }, (5000 + helpCtr.ins.fireAdd * 1000))
    }

    /**
     * 初始化元素
     */
    initEle() {

        gameCtr.ins.score = 0;
        this.overNode.active = false;
        gameCtr.ins.isOver = false;
        gameCtr.ins.gold = 120 + helpCtr.ins.goldAdd;
        gameCtr.ins.gameTime = 0;
        //创建城堡
        let c = cc.instantiate(this.castlePre);
        let spr = c.getComponent(BaseEle);
        spr.setInfo(gameCtr.ins.castleData);
        c.setPosition(cc.v2(0, 0));
        spr.setZIndex();
        this.eleNode.addChild(c);

        this.castleSpr = spr;

        //创建资源
        this.createRes();

    }

    onHelpClick() {

        if (helpCtr.ins.cannon) {
            this.createCannon(cc.v2(50, 0));
            this.createCannon(cc.v2(-50, 0));
        }

        gameCtr.ins.gold += helpCtr.ins.goldAdd;

        this.castleSpr.maxHp += helpCtr.ins.extraHp;

        this.castleSpr.hp += helpCtr.ins.extraHp;

    }

    /**
     * 创建死亡动画
     */
    async onCreateDie(e) {
        let node = gameCtr.ins.poolObj['die'].get();
        if (!node) {
            node = await cc.instantiate(this.diePre);
        }
        // console.log(e);
        if (!node) return;
        let nPos = this.bloodLayer.convertToNodeSpaceAR(e);
        node.setPosition(nPos);
        // console.log(nPos);
        node.getComponent(dieEle).setInfo();
        this.bloodLayer.addChild(node);
    }

    /**
     * 创建爆炸动画
     */
    async onCreateBaozha(e, aoeNum = 5, campType = 1) {
        let node = gameCtr.ins.poolObj['baozha'].get();
        if (!node) {
            node = await cc.instantiate(this.baozhaPre);
        }
        // console.log(e);
        let nPos = this.bloodLayer.convertToNodeSpaceAR(e);
        node.setPosition(nPos);
        // console.log(nPos);
        node.getComponent(baozhaEle).setInfo({ aoeNum: aoeNum, campType: campType });
        this.bloodLayer.addChild(node);
    }

    /**
     * 创建地刺动画
     */
    async onCreateDc(e, aoeNum = 5, campType = 1) {
        let node = gameCtr.ins.poolObj['dc'].get();
        if (!node) {
            node = await cc.instantiate(this.dcPre);
        }
        // console.log(e);
        // let nPos = this.bloodLayer.convertToNodeSpaceAR(e);
        node.setPosition(e);
        // console.log(nPos);
        node.getComponent(dcEle).setInfo({ aoeNum: aoeNum, campType: campType });
        this.bloodLayer.addChild(node);
    }

    /**
     * 创建三角阵容怪物
     */
    createtTriangleMonster() {
        let arr = [cc.v2(0, 0), cc.v2(-25, 25), cc.v2(25, 25)];
        let createPos = [cc.v2(700, Math.random() * 800 - 400), cc.v2(-700, Math.random() * 800 - 400), cc.v2(Math.random() * 1400 - 700, 400), cc.v2(Math.random() * 1400 - 700, -400)];
        let p = createPos[Math.floor(Math.random() * createPos.length)];

        for (let i = 0; i < arr.length; i++) {
            let info = {
                offX: arr[i].x,
                offY: arr[i].y,
                attack: 1,
                harvest: 0,
                hp: 1,
                type: soliderType.monster,
                campType: campType.enemy,
                isCanMove: true,
                poolName: "mon" + Math.ceil(Math.random() * 4),
                moveSpeed: 140,
                targetArr: [cc.v2(0 + arr[i].x / 2, 0 + arr[i].y / 2)],
            }
            this.createMonseter(info, cc.v2(p.x + arr[i].x, p.y + arr[i].y));
        }
    }

    /**
     * 创建正方形怪物
     * @param k 
     */
    createSquareMonster(k) {
        let arr = [];
        for (let i = 0; i < k; i++) {
            for (let j = 0; j < k; j++) {
                arr.push(cc.v2(j * 30, 30 * i));
            }
        }
        // console.log(arr);
        let createPos = [cc.v2(700, Math.random() * 800 - 400), cc.v2(-700, Math.random() * 800 - 400), cc.v2(Math.random() * 1400 - 700, 400), cc.v2(Math.random() * 1400 - 700, -400)];
        let p = createPos[Math.floor(Math.random() * createPos.length)];

        let pos1 = cc.v2((0 - p.x) + (Math.random() * 300 - 150), (0 - p.y) + (Math.random() * 300 - 150));

        let pos2 = cc.v2((0 - pos1.x) + (Math.random() * 300 - 150), (0 - pos1.y) + (Math.random() * 300 - 150));

        let pos3 = cc.v2((0 - pos2.x) + (Math.random() * 300 - 150), (0 - pos2.y) + (Math.random() * 300 - 150));

        for (let i = 0; i < arr.length; i++) {
            let info = {
                offX: arr[i].x,
                offY: arr[i].y,
                attack: 1,
                harvest: 0,
                hp: 1,
                type: soliderType.monster,
                campType: campType.enemy,
                isCanMove: true,
                poolName: "mon" + Math.ceil(Math.random() * 4),
                moveSpeed: 32,
                targetArr: [cc.v2(pos1.x + arr[i].x, pos1.y + arr[i].y), cc.v2(pos2.x + arr[i].x, pos2.y + arr[i].y), cc.v2(pos3.x + arr[i].x, pos3.y + arr[i].y), cc.v2(0 + arr[i].x / 3, 0 + arr[i].y / 3)],
            }
            // console.log(cc.v2(p.x + arr[i].x, p.y + arr[i].y))
            this.createMonseter(info, cc.v2(p.x + arr[i].x, p.y + arr[i].y));
        }
    }

    /**
     * 创建圆形阵容怪物
     */
    createCircleMonster() {
        let arr = [cc.v2(0, 0), cc.v2(0, 30), cc.v2(0, -30), cc.v2(-20, 15), cc.v2(20, 15), cc.v2(20, -15), cc.v2(-20, -15)];
        let createPos = [cc.v2(700, Math.random() * 800 - 400), cc.v2(-700, Math.random() * 800 - 400), cc.v2(Math.random() * 1400 - 700, 400), cc.v2(Math.random() * 1400 - 700, -400)]
        let p = createPos[Math.floor(Math.random() * createPos.length)];
        for (let i = 0; i < arr.length; i++) {
            let info = {
                offX: arr[i].x,
                offY: arr[i].y,
                attack: 1,
                harvest: 0,
                hp: 1,
                type: soliderType.monster,
                campType: campType.enemy,
                isCanMove: true,
                poolName: "mon" + Math.ceil(Math.random() * 4),
                moveSpeed: 60,
                targetArr: [cc.v2(0 + arr[i].x / 2, 0 + arr[i].y / 2)],
            }
            this.createMonseter(info, cc.v2(p.x + arr[i].x, p.y + arr[i].y));
        }
    }

    /**
     * 创建随机怪物
     */
    createRandMonster(k = 4) {
        let arr = [];
        for (let i = -k + 1; i < k; i++) {
            for (let j = 0; j < 2 * k - 1 - Math.abs(i); j++) {
                arr.push(cc.v2((-k + 1 + Math.abs(i) / 2 + j) * 30, 30 * i));
            }
        }

        let createPos = [cc.v2(700, Math.random() * 800 - 400), cc.v2(-700, Math.random() * 800 - 400), cc.v2(Math.random() * 1400 - 700, 400), cc.v2(Math.random() * 1400 - 700, -400)];
        let p = createPos[Math.floor(Math.random() * createPos.length)];

        let pos1 = cc.v2((0 - p.x) + Math.random() * 300 - 150, (0 - p.y) + Math.random() * 300 - 150);

        let pos2 = cc.v2((0 - pos1.x) + Math.random() * 300 - 150, (0 - pos1.y) + Math.random() * 300 - 150);

        let pos3 = cc.v2((0 - pos2.x) + Math.random() * 300 - 150, (0 - pos2.y) + Math.random() * 300 - 150);

        for (let i = 0; i < arr.length; i++) {
            let info = {
                offX: arr[i].x,
                offY: arr[i].y,
                attack: 1,
                harvest: 0,
                hp: 1,
                type: soliderType.monster,
                campType: campType.enemy,
                isCanMove: true,
                poolName: "mon" + Math.ceil(Math.random() * 4),
                moveSpeed: 32,
                targetArr: [cc.v2(pos1.x + arr[i].x, pos1.y + arr[i].y), cc.v2(pos2.x + arr[i].x, pos2.y + arr[i].y), cc.v2(pos3.x + arr[i].x, pos3.y + arr[i].y), cc.v2(0 + arr[i].x / 3, 0 + arr[i].y / 3)],
            }
            this.createMonseter(info, cc.v2(p.x + arr[i].x, p.y + arr[i].y));
        }
    }

    //创建资源
    async createRes() {
        for (let i = 0; i < 4; i++) {
            let c = gameCtr.ins.poolObj[gameCtr.ins.resData[i].poolName].get();
            if (!c) {
                if (gameCtr.ins.resData[i].poolName == "tree") {
                    c = await cc.instantiate(this.treePre);
                } else if (gameCtr.ins.resData[i].poolName == "mineral") {
                    c = await cc.instantiate(this.mineralPre);
                } else if (gameCtr.ins.resData[i].poolName == "dl") {
                    c = await cc.instantiate(this.dlPre);
                }
            }
            let spr = c.getComponent(BaseEle)
            spr.setInfo(gameCtr.ins.resData[i]);
            spr.initSp(gameCtr.ins.resData[i].iconUrl);
            c.setPosition(gameCtr.ins.resData[i].namePos);
            gameCtr.ins.resNode.addChild(c);
        }
    }

    //随机创建资源
    async createRandRes() {
        let r = Math.floor(Math.random() * gameCtr.ins.resData.length);
        let res = gameCtr.ins.resData[r];
        let c = gameCtr.ins.poolObj[res.poolName].get();
        if (!c) {
            if (res.poolName == "tree") {
                c = await cc.instantiate(this.treePre);
            } else if (res.poolName == "mineral") {
                c = await cc.instantiate(this.mineralPre);
            } else if (res.poolName == "dl") {
                c = await cc.instantiate(this.dlPre);
            }
        }
        let spr = c.getComponent(BaseEle)
        spr.setInfo(res);
        spr.initSp(res.iconUrl);
        let n = Math.random() * 380 + 150;
        let x = (Math.random() < 0.5 ? -1 : 1) * n;
        let y = Math.random() * 480 - 240;
        c.setPosition(cc.v2(x, y));
        gameCtr.ins.resNode.addChild(c);
    }

    async doCreateDl() {
        let c = gameCtr.ins.poolObj["dl"].get();
        let res = { iconUrl: "ele/dl", harvest: 0, attack: 0, hp: 30, namePos: cc.v2(-200, 190), poolName: "dl", type: soliderType.dl };
        if (!c) {
            if (res.poolName == "tree") {
                c = await cc.instantiate(this.treePre);
            } else if (res.poolName == "mineral") {
                c = await cc.instantiate(this.mineralPre);
            } else if (res.poolName == "dl") {
                c = await cc.instantiate(this.dlPre);
            }
        }
        let spr = c.getComponent(BaseEle)
        spr.setInfo(res);
        spr.initSp(res.iconUrl);
        let n = Math.random() * 380 + 150;
        let x = (Math.random() < 0.5 ? -1 : 1) * n;
        let y = Math.random() * 480 - 240;
        c.setPosition(cc.v2(x, y));

        gameCtr.ins.dlNum++;
        gameCtr.ins.resNode.addChild(c);
    }

    /**
     * 创建矿工
     * @param circle 
     */
    async createMiner(circle) {
        let node = gameCtr.ins.poolObj['miner'].get();

        if (!node) {
            node = await cc.instantiate(this.minerPre);
        }

        let x = Math.random() * 150 - 75;
        let y = Math.random() * 90 - 52;

        node.setPosition(x + circle.x, y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            id: 12,
            attack: 1, harvest: 1, hp: 5, type: soliderType.miner, isCanMove: true,
            poolName: "miner", moveSpeed: 180
        })
        spr.centerPos = circle;
    }

    /**
     * 创建守卫
     */
    async createGuard(circle, i) {
        let node = gameCtr.ins.poolObj['guard'].get();

        if (!node) {
            node = await cc.instantiate(this.guardPre);
        }
        let arr = [cc.v2(-20, 35), cc.v2(35, 5)];

        let x = arr[i].x;
        let y = arr[i].y;

        node.setPosition(x + circle.x, y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 10, type: soliderType.guard, isCanMove: true,
            poolName: "guard", id: 3
        })
        spr.centerPos = circle;
        spr.offX = x;
        spr.offY = y;
    }

    /**
     * 创建剑士
     * @param circle 
     * @param i 
     */
    async createSwordsman(circle, i) {
        let node = gameCtr.ins.poolObj['swordsman'].get();

        if (!node) {
            node = await cc.instantiate(this.swordsmanPre);
        }
        let arr = [cc.v2(0, 0), cc.v2(-20, 20), cc.v2(20, 20)];

        node.setPosition(arr[i].x + circle.x, arr[i].y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 3, type: soliderType.swordsman, isCanMove: true,
            poolName: "swordsman", id: 9
        })
        spr.centerPos = circle;
        spr.offX = arr[i].x;
        spr.offY = arr[i].y;
    }

    /**
    * 创建弓兵
    */
    async createBowman(circle, i) {
        let node = gameCtr.ins.poolObj['bowman'].get();

        if (!node) {
            node = await cc.instantiate(this.bowmanPre);
        }
        let arr = [cc.v2(0, 0), cc.v2(-25, 25), cc.v2(25, 25), cc.v2(-25, -25), cc.v2(25, -25)];

        node.setPosition(arr[i].x + circle.x, arr[i].y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 1, type: soliderType.bowman, isCanMove: true,
            poolName: "bowman", id: 8
        })
        spr.centerPos = circle;
        spr.offX = arr[i].x;
        spr.offY = arr[i].y;
    }

    /**
     * 创建骑兵
     * @param circle 
     * @param i 
     */
    async createCavalry(circle, i) {
        let node = gameCtr.ins.poolObj['cavalry'].get();
        if (!node) {
            node = await cc.instantiate(this.cavalryPre);
        }
        let arr = [cc.v2(25, -25), cc.v2(-25, 25), cc.v2(25, 25), cc.v2(-25, -25)];
        node.setPosition(arr[i].x + circle.x, arr[i].y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 8, type: soliderType.cavalry, isCanMove: true, id: 10,
            poolName: "cavalry", moveSpeed: 240
        })
        spr.centerPos = circle;
        spr.offX = arr[i].x;
        spr.offY = arr[i].y;
    }

    /**
     * 创建民兵
     */
    async createMilitia(line, i) {
        let posArr = [
            cc.v2(-18, 18,), cc.v2(-18, -18), cc.v2(0, 0),

            cc.v2(18, 18), cc.v2(18, -18),
        ]
        let node: cc.Node = gameCtr.ins.poolObj['militia'].get();

        if (!node) {
            node = await cc.instantiate(this.militiaPre);
        }
        let spr = line.getComponent(lineCom);
        let pos = this.eleNode.convertToNodeSpaceAR(spr.pos2w);

        node.setPosition(posArr[i].x + pos.x, posArr[i].y + pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            attack: 1, harvest: 0, hp: 3, type: soliderType.militia, isCanMove: true, poolName: "militia", id: 7
        })
        sprn.centerPos = line;
        sprn.offX = posArr[i].x;
        sprn.offY = posArr[i].y;
        this.eleNode.addChild(node);
    }

    /**
     * 创建盾兵
     */
    async createShield(line, i) {
        let posArr = [
            cc.v2(-13, 13,), cc.v2(-13, -13),

            cc.v2(13, 13), cc.v2(13, -13),
        ]
        let node: cc.Node = gameCtr.ins.poolObj['shield'].get();

        if (!node) {
            node = await cc.instantiate(this.shieldPre);
        }
        let spr = line.getComponent(lineCom);
        let pos = this.eleNode.convertToNodeSpaceAR(spr.pos2w);

        node.setPosition(posArr[i].x + pos.x, posArr[i].y + pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            attack: 1, harvest: 0, hp: 10, type: soliderType.shield, id: 4, isCanMove: true, poolName: "shield"
        })
        sprn.centerPos = line;
        sprn.offX = posArr[i].x;
        sprn.offY = posArr[i].y;
        this.eleNode.addChild(node);
    }

    /**
     * 创建枪兵
     */
    async createSpearman(triangle, i) {

        let posArr = [
            cc.v2(-50, 12.5), cc.v2(-50, -12.5),
            cc.v2(-25, 25,), cc.v2(-25, 0), cc.v2(-25, -25),
            cc.v2(0, 37.5), cc.v2(0, 12.5), cc.v2(0, -12.5), cc.v2(0, -37.5),
            cc.v2(25, 25,), cc.v2(25, 0), cc.v2(25, -25),
        ]
        let node: cc.Node = gameCtr.ins.poolObj['spearman'].get();

        if (!node) {
            node = await cc.instantiate(this.spearmanPre);
        }
        let spr = triangle.getComponent(triangleCom);
        let pos = this.eleNode.convertToNodeSpaceAR(spr.pos1w);

        node.setPosition(posArr[i].x + pos.x, posArr[i].y + pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            attack: 1, harvest: 0, hp: 2, type: soliderType.spearman, isCanMove: true, poolName: "spearman", id: 2
        })
        sprn.centerPos = triangle;
        sprn.offX = posArr[i].x;
        sprn.offY = posArr[i].y;
        this.eleNode.addChild(node);
    }

    /**
     * 创建怪物
     */
    async createMonseter(info, pos) {
        let node: cc.Node = gameCtr.ins.poolObj[info.poolName].get();
        // console.log(node);
        if (!node) {
            switch (info.poolName) {
                case "mon1":
                    node = await cc.instantiate(this.mon1Pre);
                    break;
                case "mon2":
                    node = await cc.instantiate(this.mon2Pre);
                    break;
                case "mon3":
                    node = await cc.instantiate(this.mon3Pre);
                    break;
                case "mon4":
                    node = await cc.instantiate(this.mon4Pre);
                    break;
            }
        }

        if (!node) return;
        if (!pos) {
            console.log("无出怪点")
        }
        if (Math.abs(pos.x) < 200 && Math.abs(pos.y) < 200) {
            console.log(pos, "出怪点过近")
        }
        node.setPosition(pos.x, pos.y);

        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo(info);

        sprn.targetPosArr = info.targetArr;
        sprn.offX = info.offX;
        sprn.offY = info.offY;
        this.eleNode.addChild(node);
    }

    /**
     * 更新金币ui
     */
    updateGold() {
        this.goldTxt.string = `${gameCtr.ins.gold}`;
        let childs = this.barracks.children;
        for (let i = 0; i < childs.length; i++) {
            let b = childs[i].getComponent(cc.Sprite);
            let role = childs[i].getChildByName("role").getComponent(cc.Sprite);
            let matName = "2d-sprite-gray";
            let color = "#009EFE";
            if (gameCtr.ins.barracksArr[i]) {
                if (gameCtr.ins.barracksArr[i].need < gameCtr.ins.gold) {
                    matName = "2d-sprite";
                    color = "#009EFE";
                } else {
                    matName = "2d-gray-sprite";
                    color = "#ffffff";
                }
                childs[i].color = new cc.Color().fromHEX(color);
                let mat = cc.Material.getBuiltinMaterial(matName)
                role.setMaterial(0, mat);
                b.setMaterial(0, mat);
            }
        }
    }

    /**
     * 点击
     * @param e 
     */
    onBarrckDown(e) {
        // console.log("onBarrckDown")
        // console.log(e.currentTarget.name)
        gameCtr.ins.selectData = null;
        let data = null;
        switch (e.currentTarget.name) {
            case "item0":
                data = gameCtr.ins.barracksArr[0];
                break;
            case "item1":
                data = gameCtr.ins.barracksArr[1];
                break;
            case "item2":
                data = gameCtr.ins.barracksArr[2];
                break;
            case "item3":
                data = gameCtr.ins.barracksArr[3];
                break;
            case "item4":
                data = gameCtr.ins.barracksArr[4];
                break;
            case "item5":
                data = gameCtr.ins.barracksArr[5];
                break;
            case "item6":
                data = gameCtr.ins.barracksArr[6];
                break;
        }
        if (gameCtr.ins.gold < data.need) return; //金币不足
        if (data == gameCtr.ins.selectData) return;
        gameCtr.ins.selectData = data;
        e.currentTarget.off(cc.Node.EventType.TOUCH_CANCEL);
        e.currentTarget.off(cc.Node.EventType.TOUCH_END);
        // let nowPos: any = e.getLocation();
        // let pos = this.node.convertToNodeSpaceAR(nowPos);

        this.mouseNode.getComponent(mouseCom).drawSp(gameCtr.ins.selectData.iconUrl);
        e.currentTarget.on(cc.Node.EventType.TOUCH_CANCEL, this.onCreateCirle, this);
        e.currentTarget.on(cc.Node.EventType.TOUCH_END, this.onCreateCirle2, this);
    }

    /**
     * 创建
     * @param touch 
     */
    onCreateCirle(touch) {
        touch.currentTarget.off(cc.Node.EventType.TOUCH_CANCEL);
        touch.currentTarget.off(cc.Node.EventType.TOUCH_END);

        let nowPos: any = touch.getLocation();
        let pos = this.node.convertToNodeSpaceAR(nowPos);
        if (pos.y < -200) {
            gameCtr.ins.selectData = null;
            this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorIdle");
            return;
        };

        this.doCreate(touch);
    }

    onCreateCirle2(touch) {
        touch.currentTarget.off(cc.Node.EventType.TOUCH_CANCEL);
        touch.currentTarget.off(cc.Node.EventType.TOUCH_END);

        gameCtr.ins.selectData = null;
        this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorIdle");
    }

    /**
     * 执行
     * @param touch 
     * @returns 
     */
    async doCreate(touch) {
        if (this.isPause) return;
        if (gameCtr.ins.isOver) return;
        if (!gameCtr.ins.selectData) return;
        // touch.currentTarget.off(cc.Node.EventType.TOUCH_START);
        if (gameCtr.ins.gold < gameCtr.ins.selectData.need) return; //金币不足
        let nowPos: any = touch.getLocation();
        let pos = this.node.convertToNodeSpaceAR(nowPos);

        let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(1);

        if (gameCtr.ins.selectData.poolName == "dogs") {
            let circle = gameCtr.ins.poolObj['circle'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circlePre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);

            circle.getComponent(circleEle).hp = barrackEny.num;

            if (gameCtr.ins.guideId == 2) {
                // let material = this.bg.getComponent(cc.Sprite).getMaterial(1);
                // this.bg.getComponent(cc.Sprite).setMaterial(0, material);
                // this.dyTxt.node.active = false;
                netCtr.ins.guide().then(() => {
                    // this.doGuide();
                });
            }

            for (let i = 0; i < barrackEny.num; i++) {
                this.createDogs(circle, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "spearman") {
            let triangle = gameCtr.ins.poolObj['triangle'].get();
            if (!triangle) {
                triangle = await cc.instantiate(this.trianglePre);
            }
            triangle.setPosition(cc.v2(0, 0));
            triangle.getComponent(triangleCom).init(pos, cc.v2(pos.x, pos.y + 120), cc.v2(pos.x + 120, pos.y));
            this.circleNode.addChild(triangle);
            // this.eleNode.addChild(triangle);

            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(2);
            triangle.getComponent(triangleCom).hp = barrackEny.num;

            if (gameCtr.ins.guideId == 3) {
                let material = this.bg.getComponent(cc.Sprite).getMaterial(1);
                this.bg.getComponent(cc.Sprite).setMaterial(0, material);
                this.dyTxt.node.active = false;
                netCtr.ins.guide().then(() => {
                    this.doGuide();
                });
            }

            for (let i = 0; i < barrackEny.num; i++) {
                this.createSpearman(triangle, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "guard") {
            let circle = gameCtr.ins.poolObj['circle3'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle3Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);
            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(3);
            circle.getComponent(circleEle).hp = barrackEny.num;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createGuard(circle, i);
            }

        } else if (gameCtr.ins.selectData['poolName'] == "shield") {
            let line = gameCtr.ins.poolObj['line'].get();
            if (!line) {
                line = await cc.instantiate(this.linePre);
            }
            line.setPosition(cc.v2(0, 0));
            line.getComponent(lineCom).init(cc.v2(0, 0), pos);
            this.circleNode.addChild(line);

            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(4);
            line.getComponent(lineCom).hp = barrackEny.num;
            line.getComponent(lineCom).isTwo = false;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createShield(line, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "cannon") {
            this.createCannon(pos);
        } else if (gameCtr.ins.selectData['poolName'] == "bomb") {
            let a = [cc.v2(80, 0), cc.v2(-80, 0), cc.v2(0, 80), cc.v2(0, -80)];
            this.onCreateBaozha(nowPos, 10, 1);
            this.scheduleOnce(() => {
                a.forEach(ele => {
                    this.onCreateBaozha(cc.v2(nowPos.x + ele.x, nowPos.y + ele.y), 10, 1);
                })
            }, 0.1)
        } else if (gameCtr.ins.selectData['poolName'] == "shell") {
            this.createShell(pos);
        } else if (gameCtr.ins.selectData['poolName'] == "militia") {
            let line = gameCtr.ins.poolObj['line'].get();
            if (!line) {
                line = await cc.instantiate(this.linePre);
            }
            line.setPosition(cc.v2(0, 0));
            line.getComponent(lineCom).init(cc.v2(pos.x, pos.y - 100), pos);
            this.circleNode.addChild(line);

            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(7);
            line.getComponent(lineCom).hp = barrackEny.num;
            line.getComponent(lineCom).isTwo = true;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createMilitia(line, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "bowman") {
            let circle = gameCtr.ins.poolObj['circle3'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle3Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);
            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(8);
            circle.getComponent(circleEle).hp = barrackEny.num;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createBowman(circle, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "swordsman") {
            let circle = gameCtr.ins.poolObj['circle3'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle2Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);
            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(9);
            circle.getComponent(circleEle).hp = barrackEny.num;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createSwordsman(circle, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "cavalry") {
            let circle = gameCtr.ins.poolObj['circle'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circlePre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);
            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);

            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(10);
            circle.getComponent(circleEle).hp = barrackEny.num;
            for (let i = 0; i < barrackEny.num; i++) {
                this.createCavalry(circle, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "trapped") {
            console.log("dddd")
            this.createTrapped(pos);
        } else if (gameCtr.ins.selectData['poolName'] == "miner") {
            let circle = gameCtr.ins.poolObj['circle2'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle2Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);
            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(12);
            circle.getComponent(circleEle).hp = barrackEny.num;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createMiner(circle);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "master") {
            let circle = gameCtr.ins.poolObj['circle3'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle3Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);

            circle.getComponent(circleEle).hp = 1;

            this.createMaster(circle);

        } else if (gameCtr.ins.selectData['poolName'] == "fighter") {
            let square = gameCtr.ins.poolObj['square'].get();
            if (!square) {
                square = await cc.instantiate(this.squarePre);
            }
            square.setPosition(cc.v2(0, 0));
            square.getComponent(squareCom).init(pos, cc.v2(pos.x + 120, pos.y), cc.v2(pos.x + 120, pos.y + 120), cc.v2(pos.x, pos.y + 120));
            this.circleNode.addChild(square);
            // this.eleNode.addChild(triangle);
            let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(14);
            square.getComponent(squareCom).hp = barrackEny.num;

            for (let i = 0; i < barrackEny.num; i++) {
                this.createFighter(square, i);
            }
        } else if (gameCtr.ins.selectData['poolName'] == "warlock") {
            // this.doCreateFrozen(pos, campType.mine);
            let circle = gameCtr.ins.poolObj['circle3'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle3Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);

            circle.getComponent(circleEle).hp = 1;

            this.createWarlock(circle);

        } else if (gameCtr.ins.selectData['poolName'] == "scarecrow") {
            this.createScarecrow(pos);
        } else if (gameCtr.ins.selectData['poolName'] == "jumpFire") {
            this.createJumpFire(pos);
        } else if (gameCtr.ins.selectData['poolName'] == "logistics") {
            let circle = gameCtr.ins.poolObj['circle3'].get();
            if (!circle) {
                circle = await cc.instantiate(this.circle3Pre);
            }
            circle.setPosition(pos);
            this.circleNode.addChild(circle);

            circle.on(cc.Node.EventType.TOUCH_START, this.onCircleDown, this);

            circle.getComponent(circleEle).hp = 1;

            this.createLogistics(circle);
        }

        gameCtr.ins.gold -= gameCtr.ins.selectData.need;
        // this.updateGold();
        gameCtr.ins.selectData = null;
        SoundResManager.getInstance().PlayBGM();
        SoundResManager.getInstance().Play("create");
        this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorIdle");
    }

    /**
     * 
     */
    async createJumpFire(pos) {
        let node: cc.Node = gameCtr.ins.poolObj['jumpFire'].get();

        if (!node) {
            node = await cc.instantiate(this.jumpFirePre);
        }
        node.setPosition(pos.x, pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.targetPosArr = [cc.v2(-540, 240), cc.v2(-540, -200)];
        let nodeIndex = 0;
        if (pos.x > 0 && pos.y > 0) {
            // nodeIndex = 3;
            sprn.targetPosArr = [cc.v2(540, 240), cc.v2(-540, 240)];
        } else if (pos.x > 0 && pos.y < 0) {
            // nodeIndex = 2;
            sprn.targetPosArr = [cc.v2(540, -200), cc.v2(540, 240)];
        }
        else if (pos.x < 0 && pos.y < 0) {
            // nodeIndex = 1;
            sprn.targetPosArr = [cc.v2(-540, -200), cc.v2(540, -240)];
        }
        sprn.setInfo({
            id: 17,
            attack: 1, harvest: 0, hp: 120, type: soliderType.jumpFire, isCanMove: true, poolName: "jumpFire", nodeIndex: nodeIndex
        })
        this.eleNode.addChild(node);
    }

    async createCannon(pos) {
        let node: cc.Node = gameCtr.ins.poolObj['cannon'].get();

        if (!node) {
            node = await cc.instantiate(this.cannonPre);
        }

        node.setPosition(pos.x, pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            attack: 1, harvest: 0, hp: 10, type: soliderType.cannon, isCanMove: false, poolName: "cannon", id: 5
        })
        node.zIndex = 999;
        this.eleNode.addChild(node);
    }

    /**
     * 创建术士
     */
    async createWarlock(circle) {
        let node = gameCtr.ins.poolObj['warlock'].get();

        if (!node) {
            node = await cc.instantiate(this.warlockPre);
        }

        let x = Math.random() * 100 - 50;
        let y = Math.random() * 60 - 35;

        node.setPosition(x + circle.x, y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 1, type: soliderType.warlock, isCanMove: true,
            poolName: "warlock", id: 15
        })
        spr.centerPos = circle;
    }

    /**
     * 创建稻草人
     */
    async createScarecrow(pos) {
        let node: cc.Node = gameCtr.ins.poolObj['scarecrow'].get();

        if (!node) {
            node = await cc.instantiate(this.scarecrowPre);
        }

        node.setPosition(pos.x, pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            attack: 1, harvest: 0, hp: 20, type: soliderType.scarecrow, isCanMove: false, poolName: "scarecrow", id: 16
        })
        this.eleNode.addChild(node);
    }

    /**
     * 创建格斗家
     * @param square 
     * @param i 
     */
    async createFighter(square, i) {
        let posArr = [
            cc.v2(-25, 25,), cc.v2(-25, -25),
            cc.v2(25, 25,), cc.v2(25, -25),
        ]
        let node: cc.Node = gameCtr.ins.poolObj['fighter'].get();

        if (!node) {
            node = await cc.instantiate(this.fighterPre);
        }
        let spr = square.getComponent(squareCom);
        let pos = this.eleNode.convertToNodeSpaceAR(spr.pos1w);

        node.setPosition(posArr[i].x + pos.x, posArr[i].y + pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            id: 14,
            attack: 1, harvest: 0, hp: 10, type: soliderType.fighter, isCanMove: true, poolName: "fighter"
        })
        sprn.centerPos = square;
        sprn.offX = posArr[i].x;
        sprn.offY = posArr[i].y;
        this.eleNode.addChild(node);
    }

    /**
     * 创建医疗兵
     * @param circle 
     */
    async createLogistics(circle) {
        let node = gameCtr.ins.poolObj['logistics'].get();

        if (!node) {
            node = await cc.instantiate(this.logisticsPre);
        }

        let x = Math.random() * 100 - 50;
        let y = Math.random() * 60 - 35;

        node.setPosition(x + circle.x, y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 1, type: soliderType.logistics, isCanMove: true,
            poolName: "logistics", id: 18
        })
        spr.centerPos = circle;
    }
    /**
     * 创建法师
     * @param circle 
     * @param i 
     */
    async createMaster(circle) {
        let node = gameCtr.ins.poolObj['master'].get();

        if (!node) {
            node = await cc.instantiate(this.masterPre);
        }

        let x = Math.random() * 100 - 50;
        let y = Math.random() * 60 - 35;

        node.setPosition(x + circle.x, y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 1, harvest: 0, hp: 2, type: soliderType.master, isCanMove: true,
            poolName: "master", id: 13
        })
        spr.centerPos = circle;
    }

    /**
     * 创建训练犬
     * @param circle 
     * @param i 
     */
    async createDogs(circle, i) {
        let node = gameCtr.ins.poolObj['dogs'].get();

        if (!node) {
            node = await cc.instantiate(this.dogsPre);
        }

        let x = Math.random() * 200 - 100;
        let y = Math.random() * 120 - 70;

        node.setPosition(x + circle.x, y + circle.y);
        this.eleNode.addChild(node);
        let spr = node.getComponent(BaseEle);
        spr.setInfo({
            attack: 0, harvest: 1, hp: 2, type: soliderType.dogs, isCanMove: true,
            poolName: "dogs", id: 1
        })
        spr.centerPos = circle;
    }

    /**
     * 创建陷阵骑士
     */
    async createTrapped(pos) {
        let node = gameCtr.ins.poolObj['trapped'].get();
        if (!node) {
            node = cc.instantiate(this.trappedPre);
        }
        node.setPosition(pos);
        let sprn = node.getComponent(BaseEle);
        let b = (700 / Math.max(Math.abs(pos.y), Math.abs(pos.x)));

        sprn.targetPosArr = [cc.v2(pos.x * b, pos.y * b)]
        sprn.setInfo({
            id: 11,
            attack: 1, harvest: 0, hp: 100, type: soliderType.trapped, isCanMove: true, poolName: "trapped",
        })
        this.eleNode.addChild(node);
    }

    /**
     * 创建地雷
     * @param pos 
     */
    async createShell(pos = null, wPos = null) {
        let node: cc.Node = gameCtr.ins.poolObj['shell'].get();
        if (!node) {
            node = await cc.instantiate(this.shellPre);
        }
        if (wPos) {
            pos = this.eleNode.convertToNodeSpaceAR(wPos);
        }

        node.setPosition(pos.x, pos.y);
        // node.angle = Math.random() * 20 - 10;
        let sprn = node.getComponent(BaseEle);
        sprn.setInfo({
            attack: 1, harvest: 0, hp: 1, type: soliderType.shell, isCanMove: false, poolName: "shell"
        })
        this.eleNode.addChild(node);
    }

    /**
     * 创建火球
     * @param pos 
     * @param targetPos 
     * @param aoeNum 
     * @param type 
     */
    async createBall(pos, targetPos, aoeNum, type: soliderType = soliderType.master) {
        if (pos && targetPos) {
            let node: cc.Node = gameCtr.ins.poolObj['fireBall'].get();

            if (!node) {
                node = await cc.instantiate(this.fireBallPre);
            }

            node.setPosition(pos.x, pos.y);

            // node.angle = Math.random() * 20 - 10;
            let sprn = node.getComponent(fireBallEle);
            sprn.setTargetPos(pos, targetPos, aoeNum, type, campType);
            this.bulletNode.addChild(node);
        }
    }

    /**
     * 创建加农炮 炮弹
     */
    async createBullet(pos, targetPos, aoeNum, type: soliderType = soliderType.cannon, campType) {
        // console.log(pos, targetPos);
        if (pos && targetPos) {
            let node: cc.Node = gameCtr.ins.poolObj['bullet'].get();

            if (!node) {
                node = await cc.instantiate(this.bulletPre);
            }

            node.setPosition(pos.x, pos.y);
            // node.angle = Math.random() * 20 - 10;
            let sprn = node.getComponent(bulletEle);
            sprn.setTargetPos(pos, targetPos, aoeNum, type, campType);
            this.bulletNode.addChild(node);
        }

    }


    /**
     * 创建 箭
     */
    async createArrow(pos, targetPos, aoeNum, type: soliderType = soliderType.cannon, campType) {
        // console.log(pos, targetPos);
        if (pos && targetPos) {
            let node: cc.Node = gameCtr.ins.poolObj['arrow'].get();

            if (!node) {
                node = await cc.instantiate(this.arrowPre);
            }

            node.setPosition(pos.x, pos.y);
            // node.angle = Math.random() * 20 - 10;
            let sprn = node.getComponent(arrowEle);
            sprn.setTargetPos(pos, targetPos, aoeNum, type, campType);
            this.bulletNode.addChild(node);
        }
    }

    /**
     * 点击圆圈
     * @param e 
     */
    onCircleDown(e) {
        e.currentTarget.on(cc.Node.EventType.TOUCH_MOVE, this.onCircleMove, this);
        e.currentTarget.color = new cc.Color().fromHEX("#ffffff");
        // console.log(e.currentTarget);
        this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorHold");
    }

    /**
     * 移动圆圈
     * @param e 
     */
    onCircleMove(e) {
        let nowPos: any = e.getLocation();
        let pos = this.circleNode.convertToNodeSpaceAR(nowPos);
        e.currentTarget.on(cc.Node.EventType.TOUCH_END, this.onCirCleUp, this);
        e.currentTarget.on(cc.Node.EventType.TOUCH_CANCEL, this.onCirCleUp, this);
        e.currentTarget.setPosition(pos);
    }

    onCirCleUp(e) {
        e.currentTarget.off(cc.Node.EventType.TOUCH_END, this.onCirCleUp, this);
        e.currentTarget.off(cc.Node.EventType.TOUCH_CANCEL, this.onCirCleUp, this);
        let colorStr = "#00DBFF";
        if (e.currentTarget.name == "circle3") {
            colorStr = "#8F57FC";
        }
        e.currentTarget.color = new cc.Color().fromHEX(colorStr);
        this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorIdle");
    }

    onMove(touch) {
        let nowPos: any = touch.getLocation();
        let pos = this.node.convertToNodeSpaceAR(nowPos);
        this.mouseNode.active = true;
        this.mouseNode.setPosition(pos);

        if (gameCtr.ins.isMoveTriangle) {
            this.triNode.active = true;
        } else {
            this.triNode.active = false;
        }
        /**
         * 判断与兵营子元素是否相交
         */
        let childs = this.barracks.children;
        this.descNode.active = false;
        for (let i = 0; i < childs.length; i++) {
            let dis = Math.sqrt(Math.pow(this.mouseNode.x - childs[i].x, 2) + Math.pow(this.mouseNode.y - childs[i].y, 2));
            // console.log(dis, "dis")
            if (dis < 40 && gameCtr.ins.barracksArr[i]) {
                this.descNode.active = true;
                this.descNode.getComponent(descComp).drawUi(gameCtr.ins.barracksArr[i], childs[i].getPosition());
                break;
            }
        }

        if (!gameCtr.ins.isFire) return;

        childs = this.eleNode.children;

        for (let i = 0; i < childs.length; i++) {
            let ele: cc.Node = childs[i];
            // console.log("hurtBlood");
            if (!ele) {
                // console.log("hurtBlood");
                continue;
            }

            let spr = ele.getComponent(BaseEle);
            if (spr.type != soliderType.monster) continue;

            if (spr.isFire) continue;

            let dis = Math.sqrt(Math.pow(this.mouseNode.x - childs[i].x, 2) + Math.pow(this.mouseNode.y - childs[i].y, 2));
            // console.log(dis, "dis")
            if (dis < 20) {
                // let dir = 1;
                // SoundResManager.getInstance().Play("fire");
                spr.doFire(nowPos);
            }
        }
    }

    onUp(touch) {

        let nowPos: any = touch.getLocation();
        let pos = this.node.convertToNodeSpaceAR(nowPos);
        gameCtr.ins.isFire = false;
        if (this.timeHandler)
            clearTimeout(this.timeHandler);

        this.lzNode.active = false;

        this.triNode.active = false;
        this.mouseNode.getComponent(mouseCom).drawSp("ele/HandCursorIdle");
        this.mouseNode.getComponent(mouseCom).playAni("mouseIdle");

        this.mouseNode.active = false;

        if (this.isPause) return;

        if (gameCtr.ins.isFire) return;

        /**
         * 判断与兵营子元素是否相交
         */
        let childs = this.eleNode.children;

        for (let i = 0; i < childs.length; i++) {
            let ele: cc.Node = childs[i];
            // console.log("hurtBlood");
            if (!ele) {
                // console.log("hurtBlood");
                continue;
            }

            if (ele.getComponent(BaseEle).type != soliderType.monster) continue;

            let dis = Math.sqrt(Math.pow(pos.x - childs[i].x, 2) + Math.pow(pos.y - childs[i].y, 2));
            // console.log(dis, "dis")
            if (dis < 20) {
                let dir = 1;
                SoundResManager.getInstance().Play("click");
                ele.getComponent(BaseEle).doFly(dir);
            }
        }
    }

    /**
     * 播放震动
     */
    doShakeAni() {
        this.shakeAni.stop();
        this.shakeAni.play("shake");
    }

    /**
     * 绘制奖励
     * @param data 
     */
    drawReward(data) {
        console.log("drawReward")
        let that = this;
        let treasureT: treasureTp = treasureCtr.ins.treasureTpDic.get(data.id);

        cc.resources.load(treasureT.iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
            if (err) return;
            that.icon.spriteFrame = sp;
            that.rewardTxt.string = `${treasureT.name}x${data.num}`;
        });
    }

    /**
     * 
     * @param dt 
     * @returns 
     */
    doGameOver() {
        this.overNode.active = true;
        this.scoreTxt.string = `${gameCtr.ins.score}`;
        this.fyTimeTxt.string = `${Math.floor(gameCtr.ins.gameTime / 60)}`;
        this.icon.spriteFrame = null;
        this.rewardTxt.string = "未获得";
        this.readyNode.active = false;
        gameCtr.ins.monsterInter = 350;
        gameCtr.ins.resTime = 0;
        gameCtr.ins.dlNum = 0;
        this.unscheduleAllCallbacks();
        this.monsterTime = false;
        // netCtr.ins.getTreasure(2);
        let exchangeList = treasureCtr.ins.exchangeTpDic.getValueList();
        let arr: exchangeTp[] = [];
        exchangeList.forEach(ele => {
            if (gameCtr.ins.score >= ele.target) {
                arr.push(ele);
            }
        })
        if (arr.length > 0) {
            netCtr.ins.getTreasure(arr.pop().rewardId, gameCtr.ins.score);
        } else {
            netCtr.ins.getTreasure(-1, gameCtr.ins.score);
        }
    }

    /**
     * 重新开始
     */
    doAgin() {
        this.eleNode.children.forEach(ele => {
            if (ele && ele.getComponent(BaseEle)) {
                ele.getComponent(BaseEle).recover();
            }
        })

        this.resNode.children.forEach(ele => {
            if (ele && ele.getComponent(BaseEle)) {
                ele.getComponent(BaseEle).recover();
            }
        })

        this.bloodLayer.children.forEach(ele => {
            if (ele && ele.getComponent(dieEle)) {
                ele.getComponent(dieEle).recover();
            }
        })

        this.eleNode.removeAllChildren();
        this.resNode.removeAllChildren();
        this.circleNode.removeAllChildren();
        this.bloodLayer.removeAllChildren();

        this.initEle();
    }

    /**
     * 返回开始场景
     */
    onBack() {
        this.readyNode.active = false;
        gameCtr.ins.monsterInter = 350;
        gameCtr.ins.resTime = 0;
        gameCtr.ins.dlNum = 0;
        this.unscheduleAllCallbacks();
        this.monsterTime = false;

        cc.find("loadNode").active = true;
        SceneManager.getInstance().loadScene("startScene", true, () => {
            cc.find("loadNode").active = false;
        });
    }

    /**
     * 点击暂停
     */
    onPause() {
        this.isPause = !this.isPause;
        let url = "ele/start";
        if (this.isPause) {
            url = "ele/start";
        } else {
            url = "ele/pause";
        }
        let that = this;
        cc.resources.load(url, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
            that.pauseBtn.getComponentInChildren(cc.Sprite).spriteFrame = sp;
        })
    }

    /**
    * 点击音乐
    */
    onMusic() {
        gameCtr.ins.isMusic = !gameCtr.ins.isMusic;

        let url = "ele/music_1";
        if (gameCtr.ins.isMusic) {
            url = "ele/music_1";
            SoundResManager.getInstance().PlayBGM();
        } else {
            url = "ele/music_2";
            SoundResManager.getInstance().StopBGM();
        }
        let that = this;
        cc.resources.load(url, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
            that.musicBtn.getComponentInChildren(cc.Sprite).spriteFrame = sp;
        })
    }

    private _count: number = 0;

    update(dt) {

        if (gameCtr.ins.isOver) return;

        if (this.isPause) return;

        if (bagCtr.ins.offerTime > 0) {
            bagCtr.ins.offerTime--;
        }

        if (bagCtr.ins.isRecover) {
            bagCtr.ins.recoverTime--;
            if (bagCtr.ins.recoverTime <= 0) {
                bagCtr.ins.recoverTime = 1000;
                this.castleSpr.recoverHp(1);
            }
        }

        if (!gameCtr.ins.isFire && gameCtr.ins.fireIntel < 900) {
            gameCtr.ins.fireIntel++;
            if (gameCtr.ins.fireIntel >= 900) {
                this.castleSpr.setFireAni("fire4");
            }
        }

        let childs = this.eleNode.children;

        /**遍历英雄元素 */
        childs.forEach(ele => {
            if (ele && ele.getComponent(BaseEle)) {
                ele.getComponent(BaseEle).onFrameLoop(dt);
            }
        })

        this.bulletNode.children.forEach(ele => {
            if (ele) {
                if (ele.getComponent(bulletEle)) {
                    ele.getComponent(bulletEle).move(dt)
                }
                if (ele.getComponent(arrowEle)) {
                    ele.getComponent(arrowEle).move(dt)
                }
                if (ele.getComponent(fireBallEle)) {
                    ele.getComponent(fireBallEle).move(dt)
                }

                if (ele.getComponent(recoverEle)) {
                    ele.getComponent(recoverEle).move(dt)
                }

                if (ele.getComponent(snowEle)) {
                    ele.getComponent(snowEle).move(dt)
                }
            }
        })

        /**随机生成资源 */
        if ((gameCtr.ins.resNode.children.length) < 2) {
            if (!this.monsterTime) {
                this.monsterTime = true;

                this.readyNode.active = true;
                this.scheduleOnce(() => {
                    this.readyNode.active = false;
                }, 4);

                gameCtr.ins.resTime = 500;
            }
        }


        if (this.monsterTime) {
            gameCtr.ins.gameTime++;

            gameCtr.ins.dlTime++;

            if (gameCtr.ins.dlTime > (1800 * helpCtr.ins.refresh) && gameCtr.ins.dlNum <= 4) {
                gameCtr.ins.dlTime = 0;
                this.doCreateDl();
            }

            gameCtr.ins.resTime++;

            if (gameCtr.ins.resTime > (900 * helpCtr.ins.refresh) && gameCtr.ins.resNode.children.length <= 8) {
                gameCtr.ins.resTime = 0;
                let num = Math.random() * 2 + 1;
                for (let i = 0; i < num; i++) {
                    this.createRandRes();
                }
            }

            this.gameTimeTxt.string = Math.floor(gameCtr.ins.gameTime / 60) + "秒";

            this._count++;

            let n = 1;
            if (gameCtr.ins.gameTime > 2400) {
                gameCtr.ins.monsterInter = 300;
            }

            if (gameCtr.ins.gameTime > 4200) {
                gameCtr.ins.monsterInter = 250;
                n = 2;
            }

            if (gameCtr.ins.gameTime > 7200) {
                gameCtr.ins.monsterInter = 250;
                n = 3;
            }

            if (gameCtr.ins.gameTime > 9000) {
                gameCtr.ins.monsterInter = 200;
                n = 3;
            }

            if (gameCtr.ins.gameTime > 12000) {
                gameCtr.ins.monsterInter = 200;
                n = 4;
            }

            if (gameCtr.ins.gameTime > 15000) {
                gameCtr.ins.monsterInter = 150;
                n = 4;
            }

            //出怪间隔时间延长
            if (bagCtr.ins.isDelay) {
                gameCtr.ins.monsterInter += 50;
            }

            if (this._count > gameCtr.ins.monsterInter) {
                this._count = 0;
                let r = gameCtr.ins.gameTime < 1200 ? 3 : 4; //前期10秒出 前面两波怪

                // rand = Math.floor(Math.random() * r);
                // console.log("rand", rand)
                for (let i = 0; i < n; i++) {

                    if (gameCtr.ins.waveNum > r) {
                        gameCtr.ins.waveNum = 0;
                    }

                    switch (gameCtr.ins.waveNum) {
                        case 0:
                            this.createtTriangleMonster();
                            break;
                        case 1:
                            this.createCircleMonster();
                            break;
                        case 2:
                            this.createtTriangleMonster();
                            break;
                        case 3:
                            let k1 = Math.floor(Math.random() * 3 + 2)
                            this.createSquareMonster(k1);
                            // this.createCircleMonster();
                            break;
                        case 4:
                            let k = Math.floor(Math.random() * 3 + 2)
                            this.createRandMonster(k);
                            break;
                    }
                    gameCtr.ins.waveNum++;
                }
            }
        }
    }

    onDjNode() {
        this.djNode.active = !this.djNode.active;
    }

    onHelpNode() {
        this.helpNode.active = !this.helpNode.active;
    }
}
